// 兼容getElementsByClassName
if (!document.getElementsByClassName) {
  document.getElementsByClassName = function(className, element) {
    var children = (element || document).getElementsByTagName("*");
    var elements = new Array();
    for (var i = 0; i < children.length; i++) {
      var child = children[i];
      var classNames = child.className.split(" ");
      for (var j = 0; j < classNames.length; j++) {
        if (classNames[j] == className) {
          elements.push(child);
          break;
        }
      }
    }
    return elements;
  };
}
// 兼容classList
if (!("classList" in document.documentElement)) {
  // 兼容indexOf方法
  if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(obj, start) {
      for (var index = start || 0, j = this.length; index < j; index++) {
        if (this[index] === obj) {
          return index;
        }
      }
      return -1;
    };
  }
  Object.defineProperty(window.Element.prototype, "classList", {
    get: function() {
      var self = this;
      function update(fn) {
        return function(value) {
          var classes = self.className.split(/\s+/g),
            index = classes.indexOf(value);
          fn(classes, index, value);
          self.className = classes.join(" ");
        };
      }
      return {
        add: update(function(classes, index, value) {
          if (!~index) classes.push(value);
        }),
        remove: update(function(classes, index) {
          if (~index) classes.splice(index, 1);
        }),
        toggle: update(function(classes, index, value) {
          if (~index) classes.splice(index, 1);
          else classes.push(value);
        }),
        contains: function(value) {
          return !!~self.className.split(/\s+/g).indexOf(value);
        },
        item: function(i) {
          return self.className.split(/\s+/g)[i] || null;
        }
      };
    }
  });
}
// 兼容placeholder
if (!("placeholder" in document.createElement("input"))) {
  var listenerName = "attachEvent";
  if ("addEventListener" in window) {
    listenerName = "addEventListener";
  }
  window[listenerName]("onload", function() {
    var placeholder = {
      //添加输入项
      add: function(tagName) {
        var list = document.getElementsByTagName(tagName);
        for (var i = 0; i < list.length; i++) {
          this.render(list[i]);
        }
        return this;
      },
      //渲染
      render: function(dom) {
        var placeholder = dom.getAttribute("placeholder");
        if (!!placeholder) {
          var placeholderDiv = this.getDiv(dom);
          placeholderDiv.innerHTML = placeholder;
          this.attachEvent(dom, placeholderDiv);
        }
      },
      //设置样式
      getDiv: function(dom) {
        var div = document.createElement("div");
        div.style.position = "absolute";
        div.style.width = this.getPosition(dom, "Width") + "px";
        div.style.height = this.getPosition(dom, "Height") + "px";
        div.style.left = this.getPosition(dom, "Left") + "px";
        div.style.top = this.getPosition(dom, "Top") + "px";
        div.style.color = "#606266";
        div.style.fontSize = "12px";
        div.style.textIndent = "5px";
        div.style.zIndex = 999;
        div.style.background = dom.style.background;
        div.style.border = dom.style.border;
        div.style.cursor = "text";
        if ("TEXTAREA" == dom.tagName.toUpperCase()) {
          div.style.lineHeight = "35px";
        } else {
          div.style.lineHeight = div.style.height;
        }
        document.getElementsByTagName("body")[0].appendChild(div);
        return div;
      },
      //计算当前输入项目的位置
      getPosition: function(dom, name, parentDepth) {
        var offsetName = "offset" + name;
        var offsetVal = dom[offsetName];
        var parentDepth = parentDepth || 0;
        if (!offsetVal && parentDepth < 3) {
          offsetVal = this.getPosition(dom.parentNode, name, ++parentDepth);
        }
        return offsetVal;
      },
      //添加事件
      attachEvent: function(dom, div) {
        //激活时，隐藏 placeholder
        dom[listenerName]("onfocus", function() {
          div.style.display = "none";
        });
        //失去焦点时，隐藏 placeholder
        dom[listenerName]("onblur", function(e) {
          if (e.srcElement.value == "") {
            div.style.display = "";
          }
        });
        //placeholder 点击时，对应的输入框激活
        div[listenerName]("onclick", function(e) {
          e.srcElement.style.display = "none";
          dom.focus();
        });
      }
    };
    //防止在 respond.min.js和html5shiv.min.js之前执行
    setTimeout(function() {
      placeholder.add("input").add("textarea");
    }, 50);
  });
}
